import App from './App'
import uviewPlus from './uni_modules/uview-plus'

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'

// 在 Vue2 下为每个页面的 onShow 增加一次角标刷新
Vue.mixin({
  onShow() {
    try {
      const pages = getCurrentPages && typeof getCurrentPages === 'function' ? getCurrentPages() : []
      const currentRoute = pages && pages.length ? pages[pages.length - 1].route : ''
      const tabbarRoutes = ['pages/index/index','pages/examine/examine','pages/message/message','pages/mine/mine']
      const isTabbar = tabbarRoutes.includes(currentRoute)
      if (!isTabbar) return

      const appInst = getApp && typeof getApp === 'function' ? getApp() : global.$app
      if (appInst?.refreshUnreadCount) appInst.refreshUnreadCount()
      if (appInst?.refreshMineDbNum) appInst.refreshMineDbNum()
      if (appInst?.refreshOaBadge) appInst.refreshOaBadge()
    } catch (e) {
      console.warn('全局 onShow 刷新角标失败:', e)
    }
  }
})

const app = new Vue({
  ...App
})
// 挂载app实例到全局，方便页面组件访问
global.$app = app
// 确保requestSubscribeMessageByUser方法可以全局访问
global.requestSubscribeMessageByUser = function() {
  if (global.$app && global.$app._isMounted) {
    return global.$app.requestSubscribeMessageByUser()
  }
}
// 确保canRequestSubscribeMessage方法可以全局访问
global.canRequestSubscribeMessage = function() {
  if (global.$app && global.$app._isMounted) {
    return global.$app.canRequestSubscribeMessage()
  }
  return false
}
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  app.use(uviewPlus)
  
  // 在 Vue3 下为每个页面的 onShow 增加一次角标刷新
  app.mixin({
    onShow() {
      try {
        if(uni.getStorageSync('token')){
          const pages = getCurrentPages && typeof getCurrentPages === 'function' ? getCurrentPages() : []
          const currentRoute = pages && pages.length ? pages[pages.length - 1].route : ''
          const tabbarRoutes = ['pages/index/index','pages/examine/examine','pages/message/message','pages/mine/mine']
          const isTabbar = tabbarRoutes.includes(currentRoute)
          if (!isTabbar) return

          const appInst = getApp && typeof getApp === 'function' ? getApp() : null
          if (appInst?.refreshUnreadCount) appInst.refreshUnreadCount()
          if (appInst?.refreshMineDbNum) appInst.refreshMineDbNum()
          if (appInst?.refreshOaBadge) appInst.refreshOaBadge()
        }
      } catch (e) {
        console.warn('全局 onShow 刷新角标失败:', e)
      }
    }
  })
  
  // 挂载方法到全局，方便页面组件访问
  global.$app = { globalData: {} }
  // 确保requestSubscribeMessageByUser方法可以全局访问
  global.requestSubscribeMessageByUser = function() {
    // 在Vue 3中，实际调用会在App实例创建后通过proxy来处理
    console.log('Vue 3 - 请求订阅消息权限（需要用户点击触发）')
  }
  // 确保canRequestSubscribeMessage方法可以全局访问
  global.canRequestSubscribeMessage = function() {
    return true // 默认返回true，实际逻辑会在App实例中实现
  }
  
  return {
    app
  }
}
// #endif










